package com.springboot.last_experiment.service;

import com.springboot.last_experiment.dao.AdminDao;
import com.springboot.last_experiment.entity.Admin;
import com.springboot.last_experiment.vo.ResultVO;
import com.springboot.last_experiment.voutils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class AdminService implements UserDetailsService {

    @Autowired
    AdminDao adminDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println(s);
        Admin admin = adminDao.getByName(s);
        System.out.println("我进入server了");
        System.out.println(admin);
        return new org.springframework.security.core.userdetails.User(s,
            admin.getPassword(),
            true,
            true,
            true,
            true,
            getGrantedAuthority(admin));

    }
    public List<GrantedAuthority> getGrantedAuthority(Admin admin){
        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        return list;
    }

    public ResultVO admin_login(String name, String password){
        Admin passByName = adminDao.getByName(name);
        if(passByName==null){
            return ResultVOUtil.error(500,"没有该账号");
        }
        else if(passByName.getAdminPass().equals(password)){
            return ResultVOUtil.success(passByName);
        }else {
            return ResultVOUtil.error(500,"密码错误");
        }
    }

    public ResultVO getAllAdm(){
        return ResultVOUtil.success(adminDao.getAllAdm());
    }

    public Admin getAdmById(Integer id){
        return adminDao.getAdmById(id);
    }

    public ResultVO getAdmByKey(String key){
        switch (key){
            case "未知":key="0";break;
            case "男":key="1";break;
            case "女":key="2";break;
            default:break;
        }
        return ResultVOUtil.success(adminDao.getAdmByKey(key));
    }

    public ResultVO deleteAdm(Integer id) {
        try {
            adminDao.deleteAdm(id);
        }catch (Exception e){
            return ResultVOUtil.error(500,"删除异常");
        }
        return ResultVOUtil.success(null);
    }

    public ResultVO insertAdm(Admin admin){
        admin.setAdminRole(1);
        try{
            adminDao.insertAdm(admin);
        }catch (Exception e){
            return ResultVOUtil.error(500,"添加异常");
        }
        return ResultVOUtil.success(null);
    }

    public ResultVO updateAdm(Admin admin){
        try {
            adminDao.updateAdm(admin);
        }catch (Exception e){
            return ResultVOUtil.error(500,"修改异常");
        }
        return ResultVOUtil.success(null);
    }


}
